package com.atfzr;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author 望夜
 * @date 2023/8/13---21:38
 * @SuppressWarnings({"all"})
 */
public class test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<String>(() -> {
            System.out.println(Thread.currentThread().getName() + "\t ----come in");
            //暂停几秒线程
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task over";
        });
        new Thread(futureTask,"t1").start();

        System.out.println(Thread.currentThread().getName() + "\t ----忙其它任务了");
//        System.out.println(futureTask.get()); //会阻塞主线程，同步导致阻塞5s
        while (true){
            if(futureTask.isDone()){ //轮询
                System.out.println(futureTask.get());
                break;
            }else{
                //暂停毫秒
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("正在处理中，不要再催了，越催越慢，再催熄火");
            }
        }
    }

}
